import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
 * Write a description of class SimpleTower here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SimpleTower extends TOWER
{
    public SimpleTower(int x_p, int y_p)
    {
        super(x_p,y_p);
        size = 20;
        radius = 200;
        damage = 30;
        rate = 30;
        count = rate;
        isselected = false;
        drawRange = true;
        upgradelevel = 1;
        cost = 16;
        upgradecost = 2;
    }
    public void paint(Graphics g)
    {
        g.drawRect(x,y,size,size);
        for(int i = 1; i <= upgradelevel; i++)
        {
            if(template == false)
            {
                g.drawRect(x-4+i*4, y, 4, 4);
            }
        }
        if(isselected == true)
        {
            g.setColor(Color.green);
            g.drawRect(x-1, y-1, size+2, size+2);
            g.drawRect(x-2, y-2, size+4, size+4);
            g.setColor(Color.black);
        }
        if(count == 0) g.drawLine(x+size/2,y+size/2,creepx,creepy);
        if((drawRange == true) && (isselected == true))
        {
            g.drawOval(x+size/2-radius,y+size/2-radius,radius*2,radius*2);
        }
    }
    public void work(ArrayList<CREEP> creeps)
    {
        if(count == 1)
        {
            if(!creeps.isEmpty())
            {
                int bestdistance = Integer.MAX_VALUE;
                CREEP bestcreep = null;
                for(CREEP c : creeps)
                {
                    int dist = (int)Math.sqrt(Math.pow(c.getxPos()-x+size/2,2)+Math.pow(c.getyPos()-y+size/2,2));
                    if(dist<bestdistance) 
                    {
                        bestdistance = dist;
                        bestcreep = c;
                    }
                }
                if(bestdistance>radius) bestcreep = null;
                if(bestcreep != null)
                {
                    bestcreep.addDamage(damage);
                    creepx = bestcreep.getxPos();
                    creepy = bestcreep.getyPos();
                    count = 0;
                }
            }
        }
        else
        {
            count = (count==0)?rate:count-1;
        }
    }
    public int sell()
    {
        return (cost+upgradelevel*upgradecost-upgradecost)/2;
    }
    public void upgrade()
    {
        switch(upgradelevel)
        {
            case 1: 
            radius = 210;
            damage = 35;
            upgradelevel++;
            break;
            
            case 2:
            radius = 220;
            damage = 40;
            upgradelevel++;
            break;
            
            case 3:
            radius = 230;
            damage = 45;
            upgradelevel++;
            break;
            
            case 4:
            radius = 240;
            damage = 50;
            upgradelevel++;
            break;
        }
    }
}
